﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using RungSoft.RungEBookMaker.Providers.DocFormater.Exceptions;

namespace RungSoft.RungEBookMaker.Providers.DocFormater.Umd
{
    [Serializable]
    internal class BookBM : BookBase
    {
        // Fields
        private List<ChapterBM> mChapters = new List<ChapterBM>();

        /// <summary>
        /// 添加一个章节的内容。
        /// 为维护章节的有序性，建议使用此方法而不是直接调用 Chapters 属性的 Add 方法。
        /// </summary>
        internal void AppendChapter(ChapterBM chapter)
        {
            chapter.Index = this.mChapters.Count;
            this.mChapters.Add(chapter);
        }

        /// <summary>
        /// 移除一个章节的内容。
        /// 为维护章节的有序性，建议使用此方法而不是直接调用 Chapters 属性的 Remove 方法。
        /// </summary>
        internal void RemoveChapter(ChapterBM chapter)
        {
            int index = chapter.Index;
            if (this.mChapters.Remove(chapter))
            {
                for (int i = chapter.Index; i < this.mChapters.Count; i++)
                {
                    this.mChapters[i].Index = i;
                }
            }
        }

        /// <summary>
        /// 验证该书籍信息是否已经完备，并且可以被保存。
        /// </summary>
        internal string CanSave()
        {
            if ((this.Title == null) || (this.Title.Length < 1))
            {
                return "标题不能为空！";
            }
            if ((this.Author == null) || (this.Author.Length < 1))
            {
                return "作者不能为空！";
            }
            if ((this.Chapters == null) || (this.Chapters.Count < 1))
            {
                return "内容数量不能小于0！";
            }
            if ((this.FilePath == null) || (this.FilePath.Length < 1))
            {
                return "保存文件的路径不正确！";
            }
            if (File.Exists(this.FilePath))
            {
                try
                {
                    File.Delete(this.FilePath);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    return "无法覆盖，文件正在被使用！";
                }
            }
            return null;
        }

        /// <summary>
        /// 书籍格式（文本型、漫画型、连环画型）
        /// </summary>
        internal byte[] Formate
        {
            get;
            set;
        }

        /// <summary>
        /// 书籍类别（文艺类、军事类等）
        /// </summary>
        internal string Kind
        {
            get;
            set;
        }

        /// <summary>
        /// 发行者
        /// </summary>
        internal string Publisher
        {
            get;
            set;
        }

        /// <summary>
        /// 贩卖商
        /// </summary>
        internal string Vendor
        {
            get;
            set;
        }

        /// <summary>
        /// 封面
        /// </summary>
        internal Image Cover
        {
            get;
            set;
        }

        /// <summary>
        /// 书籍章节集合
        /// </summary>
        internal List<ChapterBM> Chapters
        {
            get
            {
                return this.mChapters;
            }
        }

        internal override void LoadByProject(string filePath)
        {
            base.LoadByProject(filePath);

            BookBM bBM = this.mProjectBM.Book as BookBM;
            if (this.mProjectBM == null) throw (new ProjectFormateException("项目文件格式错误（2）。"));

            this.mChapters = bBM.mChapters;
            this.Formate = bBM.Formate;
            this.Kind = bBM.Kind;
            this.Publisher = bBM.Publisher;
            this.Vendor = bBM.Vendor;
            this.Cover = bBM.Cover;
        }
    }
}

